package com.six.backend.service.impl;

import com.six.backend.dao.RoleDao;
import com.six.backend.pojo.Authority;
import com.six.backend.pojo.Role;
import com.six.backend.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: skyler
 * @Date: 2021/11/7 13:01
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    /**
     * 权限级别数
     */
    private static final Integer TYPE_NUM = 3;


    @Override
    public Set<String> selectRoleNameByAccount(String account) {
        return roleDao.selectRoleByAccount(account);
    }

    @Override
    public List<Role> selectRoles(Map map) {
        return roleDao.selectRoles(map);
    }

    @Override
    public Boolean insertRole(Map map) {
        if (roleDao.insertRole(map) == 1) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean isExist(String name) {
        return !roleDao.isExist(name).isEmpty();
    }

    @Override
    public Boolean updateRole(Role role) {
        if (roleDao.updateRole(role) != 0) {
            return roleDao.updateRoleNameInRoleAuthority(role.getName()) >= 0 && roleDao.updateRoleNameInUserRole(role.getName()) >= 0;
        }
        return false;
    }

    @Override
    public List<Authority> selectPermissionByRoleName(String roleName) {
        List<List<Authority>> list = new ArrayList();
        // 获取各级权限
        for (int i = 0; i < TYPE_NUM; i++) {
            list.add(roleDao.selectPermissionByRoleName(roleName,i));
        }
        // 装配各级权限
        for (int i = 0; i < list.size()-1; i++) {
            setPermissionToTree(list.get(i),list.get(i+1));
        }
        return list.get(0);
    }

    @Override
    public List<Authority> selectOtherPermissionByRoleName(String roleName) {
        List<List<Authority>> list = new ArrayList();
        // 获取各级权限
        for (int i = 0; i < TYPE_NUM; i++) {
            list.add(roleDao.selectOtherPermissionByRoleName(roleName,i));
        }
        // 装配各级权限
        for (int i = 0; i < list.size()-1; i++) {
            setPermissionToTree(list.get(i),list.get(i+1));
        }
        return list.get(0);
    }

    @Override
    public Boolean insertPermissionOfRoleName(String roleName, List<Authority> authorityList) {
        Map map = new HashMap();
        map.put("roleName",roleName);
        ArrayList list = new ArrayList();
        for (Authority authority : authorityList) {
            list.add(authority.getId());
            for (Authority child : authority.getChildren()) {
                list.add(child.getId());
            }
        }
        map.put("list",list);
        return roleDao.insertPermissionOfRoleName(map) >= 0;
    }

    @Override
    public Boolean editPermissionOfRoleName(String roleName, List<Authority> authorityList) {
        return roleDao.deleteAllPermissionOfRoleName(roleName) >= 0 && insertPermissionOfRoleName(roleName,authorityList);
    }

    @Override
    public Boolean deleteRole(String roleName) {
        return roleDao.deleteRole(roleName) > 0;
    }

    @Override
    public Boolean isRoleUsed(String roleName) {
        return roleDao.selectUsersUsingRole(roleName).size() > 0;
    }


    /**
     * 生成权限树
     * @param father
     * @param son
     */
    private void setPermissionToTree(List<Authority> father, List<Authority> son) {
        for (Authority father1 : father) {
            List<Authority> authorities = new ArrayList<>();
            for (Authority son1 : son) {
                if (son1.getParentId().equals(father1.getId())) {
                    authorities.add(son1);
                }
            }
            father1.setChildren(authorities);
        }
    }





}
